import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.MemoryImageSource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import imagereader.IImageIO;

public class ImplementImageIO implements IImageIO {
	/**
	 * override the myRead method
	 * @param filepath: the path of file
	 * @return Image
	 */
	@Override
	public Image myRead(String filepath) throws IOException {
		// TODO Auto-generated method stub
		byte[] bitmapHead = new byte[14];
		byte[] bitmapInfo = new byte[40];
		int headSize, infoSize, width, height, depthOfColor, blue, green, red;
		
		File file = new File(filepath);
		
		try {
			FileInputStream fileInputStream = new FileInputStream(file);
			fileInputStream.read(bitmapHead, 0, 14);
			fileInputStream.read(bitmapInfo, 0, 40);
			//headSize and infoSize are not used but important
			headSize = (bitmapHead[2] & 0xff) | ((bitmapHead[3] & 0xff) << 8) | ((bitmapHead[4] & 0xff) << 16) | ((bitmapHead[5] & 0xff) << 24);
			infoSize = (bitmapInfo[20] & 0xff) | ((bitmapInfo[21] & 0xff) << 8) | ((bitmapInfo[22] & 0xff) << 16) | ((bitmapInfo[23] & 0xff) << 24);
			width = (bitmapInfo[4] & 0xff) | ((bitmapInfo[5] & 0xff) << 8) | ((bitmapInfo[6] & 0xff) << 16) | ((bitmapInfo[7] & 0xff) << 24);
			height = (bitmapInfo[8] & 0xff) | ((bitmapInfo[9] & 0xff) << 8) | ((bitmapInfo[10] & 0xff) << 16) | ((bitmapInfo[11] & 0xff) << 24);
			depthOfColor = (bitmapInfo[14] & 0xff) | ((bitmapInfo[15] & 0xff) << 8);
			
			int skip = (depthOfColor * width / 8) % 4;
			if (skip != 0) {
				skip = 4 - skip;
			}
			if (depthOfColor == 24) {
				int data[] = new int[height * width];
				for (int i = height - 1; i >= 0; i--) {
					for (int j = 0; j < width; j++) {
						blue = fileInputStream.read();
						green = fileInputStream.read();
						red = fileInputStream.read();
						Color color = new Color(red, green, blue);
						data[i * width + j] = color.getRGB();
					}
					if (skip != 0) {
						fileInputStream.skip(skip);
					}
				}
				fileInputStream.close();
				return Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(width, height, data, 0, width));	
			}
			else {
				fileInputStream.close();
				return null;
			}
		}
		catch (Exception e) {
			return null;
		}
	}

	/**
	 * override the myWrite method
	 * @param filepath: the path of file
	 * @param image: the image to write
	 * @return Image
	 */
	@Override
	public Image myWrite(Image image, String filePath) throws IOException {
		// TODO Auto-generated method stub
		try {
			File file = new File(filePath);
			BufferedImage bitmapImage;
			bitmapImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
			Graphics2D Gr = bitmapImage.createGraphics();
			Gr.drawImage(image, 0, 0, null);
			Gr.dispose();
			//use java API to write bmp file
			ImageIO.write(bitmapImage, "bmp", file);		
		}
		catch (Exception e) {
			return null;
		}
		return null;
	}
}
